<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Mod_article extends MYAD_Controller{
        protected $module_name = "article"; 
        function __construct(){
            parent::__construct();
            $this->load->model(array('Mmod_article','Cmmod_article'));
        }
        function index(){
            $data['title'] = 'Quản lý bài viết';
            $data['title_table'] = 'Quản lý bài viết';
            $data['show']="index";
            $data['data'] = $this->Mmod_article->listAll();
            foreach($data['data'] as $key=>$row){
                $data['data'][$key]['cat_name'] = $this->getNameCat($row['c_id']);
            }                
            $this->load->view('ad_layout',$data);
        }
        function add(){
            $this->load->helper('seourl');
            if(isset($_REQUEST['submit'])){
                $name = $_REQUEST['name'];
                $url = seo_url($_REQUEST['name']);
                $parent_id = $_REQUEST['parent_id'];
                $title = $_REQUEST['title']!=NULL?$_REQUEST['title']:$name;
                $des = $_REQUEST['des']!=NULL?$_REQUEST['des']:$name;
                $sumary = $_REQUEST['sumary'];
                if($_FILES['photo']['name'] != NULL){
                    $this->upload_images("photo",$this->module_name);
                    $photo = $_FILES["photo"]["name"];
                }
                else{
                    $photo = "";
                }
                $content = $_REQUEST['content'];
                $tags = $_REQUEST['tags'];
 		        $order= $_REQUEST['order'];
                $status = isset($_REQUEST['status'])?1:0;							
                $data = array(
                   'c_id' => $parent_id ,
                   'name' => $name ,
                   'url' => $url ,
                   'title' => $title ,
                   'des' => $des ,
                   'sumary' => $sumary ,
                   'photo' => $photo ,
                   'content' => $content ,
                   'tags' => $tags ,
                   'order' => $order ,                   
                   'status' => $status ,                   				   				   
                );
                $this->Mmod_article->insertData($data);
                $this->session->set_flashdata('message', 'Thêm bài viết thành công!');
                redirect('/quantri/article');                
            }
            else{
                $data['title'] = 'Quản lý bài viết - Thêm bài viết';
                $data['title_table'] = 'Quản lý bài viết - Thêm bài viết';
                $data['show']="add";
                $data['category'] = $this->get_category(0);              
                $this->load->view('ad_layout',$data);                
            }           
        }
        function edit($id){
            $this->load->helper('seourl');  
            if($id){
                if(isset($_REQUEST['submit'])){
                    $name = $_REQUEST['name'];
                    $url = seo_url($_REQUEST['name']);
                    $parent_id = $_REQUEST['parent_id'];
                    $title = $_REQUEST['title']!=NULL?$_REQUEST['title']:$name;
                    $des = $_REQUEST['des']!=NULL?$_REQUEST['des']:$name;
                    $sumary = $_REQUEST['sumary'];
                    if($_FILES['photo']['name']!= NULL){
                        // xoa anh truoc khi cap nhat anh moi
                        $filename = realpath(APPPATH. "../public/uploads")."/".$this->module_name."/".$_REQUEST['edit_photo'];
                        $thumb = realpath(APPPATH. "../public/uploads")."/".$this->module_name."/thumbs/".$_REQUEST['edit_photo'];
                        unlink($thumb);
                        unlink($filename);
                        // upload anh moi                   
                        $this->upload_images("photo",$this->module_name);
                        $photo = $_FILES["photo"]["name"];
                    }
                    else{
                        $photo = $_REQUEST['edit_photo'];
                    }
                    $content = $_REQUEST['content'];
                    $tags = $_REQUEST['tags'];
     		        $order= $_REQUEST['order'];
                    $id = $_REQUEST['id'];
                    $status = isset($_REQUEST['status'])?1:0;							
                    $data = array(
                       'c_id' => $parent_id ,
                       'name' => $name ,
                       'url' => $url ,
                       'title' => $title ,
                       'des' => $des ,
                       'sumary' => $sumary ,
                       'photo' => $photo ,
                       'content' => $content ,
                       'tags' => $tags ,
                       'order' => $order ,                   
                       'status' => $status ,                   				   				   
                    );
                    $this->Mmod_article->updateData($data,$id);
                    $this->session->set_flashdata('message', 'Chỉnh sửa bài viết thành công!');
                    redirect('/quantri/article');       
                }
                else{
                    $data['title'] = 'Quản lý bài viết';
                    $data['title_table'] = 'Chỉnh sửa bài viết';
                    $data['show']="edit";
                    $data['data'] = $this->Mmod_article->listId($id);
                    foreach($data['data'] as $row){
                        if($row['c_id']){
                            $data['category'] = $this->get_category_selected(0,$row['c_id']);
                        }
                    }                 
                    $this->load->view('ad_layout',$data);
                }
            }           
        }
		function del($id){
		  if($id){
                // delete images article
                $filename = realpath(APPPATH. "../public/uploads")."/article/".$_POST['photo'];
                $thumb = realpath(APPPATH. "../public/uploads")."/article/thumbs/".$_POST['photo'];
                unlink($filename);
                unlink($thumb);
                $this->Mmod_article->deleteData($id);
                echo "ok";
            }
		} 
        /*
            ham nay dung de lay ten cua category 
        */
        function getNameCat($idCat){
            $row = $this->Mmod_article->getCatNameById($idCat);
            if(isset($row['name']))
                return $row['name'];
        } 
        /*
        ham lay toan bo danh muc category 4 cap
        */
        function get_category($idParent){
            $result = $this->Cmmod_article->getAllChild($idParent);
            foreach($result as $row){
                $this->html .= "<option value='".$row['id']."'>".$row['name']."</option>";
                if($this->Cmmod_article->getNumRowCat($row['id'])){
                    
                    $result1 = $this->Cmmod_article->getAllChild($row['id']);
                    foreach($result1 as $row1){
                        $this->html .= "<option value='".$row1['id']."'>----".$row1['name']."</option>";
                        if($this->Cmmod_article->getNumRowCat($row1['id'])){
                            $result2 = $this->Cmmod_article->getAllChild($row1['id']);
                            foreach($result2 as $row2){
                                $this->html .= "<option value='".$row2['id']."'>--------".$row2['name']."</option>";
                                 if($this->Cmmod_article->getNumRowCat($row2['id'])){
                                 
                                    $result3 = $this->Cmmod_article->getAllChild($row2['id']);
                                    foreach($result3 as $row3){
                                        $this->html .= "<option value='".$row3['id']."'>------------".$row3['name']."</option>";
                                    }                                 
                                 
                                 }
                            } 
                                                        
                        }
                    } 
                                       
                }
            }
            return $this->html;
        }
        /*
        ham lay toan bo danh muc category 4 cap trong do co 1 danh muc duoc chon
        */        
        function get_category_selected($idParent,$idSelected){
            $result = $this->Cmmod_article->getAllChild($idParent);
            foreach($result as $row){
                $this->html .= $idSelected;
                if($row['id']==$idSelected){
                    $this->html .= "<option value='".$row['id']."' selected='selected' >".$row['name']."</option>";
                }
                else{
                    $this->html .= "<option value='".$row['id']."' >".$row['name']."</option>";
                }
                if($this->Cmmod_article->getNumRowCat($row['id'])){
                    
                    $result1 = $this->Cmmod_article->getAllChild($row['id']);
                    foreach($result1 as $row1){
                        if($row1['id']==$idSelected){
                            $this->html .= "<option value='".$row1['id']."' selected='selected' >----".$row1['name']."</option>";
                        }
                        else{
                           $this->html .= "<option value='".$row1['id']."'>----".$row1['name']."</option>";
                        }                        
                        
                        if($this->Cmmod_article->getNumRowCat($row1['id'])){
                            $result2 = $this->Cmmod_article->getAllChild($row1['id']);
                            foreach($result2 as $row2){
                                if($row2['id']==$idSelected){
                                    $this->html .= "<option value='".$row2['id']."' selected='selected' >--------".$row2['name']."</option>";
                                }
                                else{
                                   $this->html .= "<option value='".$row2['id']."'>--------".$row2['name']."</option>";
                                }
                                 if($this->Cmmod_article->getNumRowCat($row2['id'])){
                                 
                                    $result3 = $this->Cmmod_article->getAllChild($row2['id']);
                                    foreach($result3 as $row3){
                                        if($row3['id']==$idSelected){
                                            $this->html .= "<option value='".$row3['id']."' selected='selected' >------------".$row3['name']."</option>";
                                        }
                                        else{
                                           $this->html .= "<option value='".$row3['id']."'>------------".$row3['name']."</option>";
                                        }
                                    }                                 
                                 
                                 }
                            } 
                                                        
                        }
                    } 
                                       
                }
            }
            return $this->html;
        }               
    }
?>